﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.Unity;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using System.Web.SessionState;
using SZHL.UI.AppCode;
using SZHL.Data;

namespace SZHL.UI.API
{
    /// <summary>
    /// FTAPI 的摘要说明
    /// </summary>
    public class FTAPI : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            context.Response.ContentType = "text/plain";
            string strAction = context.Request["Action"] ?? "";
            // string strUserName = "";
            Msg_Result Model = new Msg_Result() { Action = strAction.ToUpper(), ErrorMsg = "" };
            string P1 = context.Request["P1"] ?? "";
            string P2 = context.Request["P2"] ?? "";
            if (!string.IsNullOrEmpty(strAction))
            {
                try
                {


                    //TODO: 未实现,分享页面的接口通过暂时将分享人的code传递给打开链接的人来调用接口(不安全)

                    #region 必须登录执行接口
                    Model.ErrorMsg = "";
                    string strUserName = string.Empty;
                    if (context.Request.Cookies["szhlcode"] != null)
                    {
                        //通过Cookies获取Code
                        string szhlcode = context.Request.Cookies["szhlcode"].Value;
                        //通过Code获取用户名，然后执行接口方法
                        var jau = new SZHL.UI.AppCode.JH_Auth_UserB().GetEntity(p => p.pccode == szhlcode);
                        if (jau == null)
                        {
                            jau = new SZHL.UI.AppCode.JH_Auth_UserB().GetEntity(p => p.mobcode == szhlcode);
                        }
                        if (jau != null)
                        {
                            var container = ServiceContainer.Current().Resolve<IWsService>(strAction.ToUpper());
                            JH_Auth_UserB.UserInfo UserInfo = new JH_Auth_UserB.UserInfo(jau.UserName);
                            container.ProcessRequest(context, ref Model, P1, P2, UserInfo);
                        }
                        else
                        {
                            Model.ErrorMsg = "NOSESSIONCODE";
                        }
                    }
                    else if (context.Request["sharecode"] != null)
                    {
                        //通过Cookies获取Code
                        string sharecode = context.Request["P1"].ToString();
                        string sharepasd = context.Request["P2"].ToString();
                        if (strAction == "GetShareItem")
                        {
                            //通过Code获取用户名，然后执行接口方法
                            FT_File File = new FT_File();
                            FT_Folder Folder = new SZHL.UI.AppCode.FT_FolderB().GetEntity(p => p.ShareCode == sharecode && p.SharePasd == sharepasd);
                            if (Folder == null)
                            {
                                File = new SZHL.UI.AppCode.FT_FileB().GetEntity(p => p.ShareCode == sharecode && p.SharePasd == sharepasd);
                                Model.Result = File;
                            }
                            else
                            {
                                Model.Result = Folder;
                            }
                        }
                        if (strAction == "GetShareData")
                        {
                            //通过Code获取用户名，然后执行接口方法
                            Model.Result = new FT_FolderB().GetEntities(d => d.PFolderID.ToString() == P1);
                            Model.Result1 = new FT_FileB().GetEntities(d => d.FolderID.ToString() == P1);
                        }
                    }
                    else
                    {
                        Model.ErrorMsg = "NOSESSIONCODE";
                    }
                    #endregion

                }
                catch (Exception ex)
                {
                    Model.ErrorMsg = "操作异常，请重试";
                    Model.Result = ex.Message;
                }

            }
            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
            timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string Result = JsonConvert.SerializeObject(Model, Formatting.Indented, timeConverter).Replace("null", "\"\"");
            context.Response.Write(Result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}